package org.example.adminservice.config;

import org.example.Response.ApiResponse;
import org.example.Response.ApiResponseCode;
import org.example.Response.ApiResponseUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public ResponseEntity<ApiResponse<Object>> handleNoHandlerFoundException(NoHandlerFoundException ex) {
        ApiResponse<Object> resp =   ApiResponseUtil.error(ApiResponseCode.NOT_FOUND);
        return new ResponseEntity<>(resp, HttpStatus.NOT_FOUND);
    }

    // 可以增加其他的异常处理，比如 500 错误等
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ApiResponse<Object>> handleGlobalException(Exception ex) {
        ApiResponse<Object> resp =   ApiResponseUtil.error("Internal Server Error"+ ex.getMessage());
        System.out.println("Internal Server Error");
        return new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
